package com.tg.app.activity.device;

import static com.tg.app.activity.device.list.DeviceListPushConfig.OPEN_DRIVE_LOCAL_PALYBACK_SDCARD;

import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.appbase.custom.constant.CommonConstants;
import com.appbase.custom.constant.DevcieFunctionConstants;
import com.tange.base.toolkit.StringUtils;
import com.tange.module.camera.hub.CameraHub;
import com.tg.app.R;
import com.tg.app.activity.CloudServiceActivity;
import com.tg.app.adapter.DeviceAdapter;
import com.tg.app.adapter.DeviceBusinessAdapter;
import com.tg.app.helper.ActivityHelper;
import com.tg.app.helper.DeviceHelper;
import com.tg.appcommon.android.AbAppUtil;
import com.tg.appcommon.business.IMessageModule;
import com.tg.data.bean.DeviceItem;

import java.util.ArrayList;
import java.util.List;

public class DeviceBusinessActivity extends FragmentActivity {
    public static final int RESULT_PERMISSION = 10;
    private RecyclerView recyclerView;
    private List<Integer> functionList = new ArrayList<>();
    private DeviceBusinessAdapter deviceBusinessAdapter;
    private DeviceItem deviceItem;
    private ArrayList<DeviceItem> deviceItems;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_business);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);//需要添加的语句
        deviceItem = getIntent().getParcelableExtra(CommonConstants.EXT_DEVICE_ITEM);
        deviceItems = getIntent().getParcelableArrayListExtra(IMessageModule.EXT_EVENT_DEVICES);
        initView();
        functionList.clear();

        if (deviceItem != null) {
            if (!CameraHub.getInstance().isEnableLocalApConnect()){
                functionList.add(DevcieFunctionConstants.CARD_VIDEO);
            }
            functionList.add(DevcieFunctionConstants.SETTINGS);
        }
        findViewById(R.id.device_business_layout).setOnClickListener(v -> finish());
        deviceBusinessAdapter.notifyDataSetChanged();
    }


    private void initView() {
        recyclerView = findViewById(R.id.recyclerview_device_business);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 1);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        deviceBusinessAdapter = new DeviceBusinessAdapter(functionList);
        recyclerView.setAdapter(deviceBusinessAdapter);
        deviceBusinessAdapter.setDeviceItem(deviceItem);
        deviceBusinessAdapter.setOnItemClickListener(new DeviceAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, int type) {
                switch (type) {
                    case  DevcieFunctionConstants.SERVICE_4G:
                        openWebService(CloudServiceActivity.EXT_CLOUD_SERVICE_TYPE_SIM);
                        break;
                    case DevcieFunctionConstants.CARD_VIDEO:
                        openCardVideo();
                        break;
                    case  DevcieFunctionConstants.SETTINGS:
                        openSettings();
                        break;
                }

                finish();
            }

            @Override
            public void onUpdate() {

            }

        });
    }

    private void openSettings(){
        Intent intent = new Intent();
        intent.setClass(this, DeviceSettingsActivity.class);
        intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, deviceItem);
        intent.putExtra(CommonConstants.EXT_PUSH_ORIGIN, DeviceListActivity.PUSH_TARGET_LIST);
        startActivity(intent);
    }


    private void openCardVideo(){
        DeviceHelper.openDevice(DeviceBusinessActivity.this, deviceItem, OPEN_DRIVE_LOCAL_PALYBACK_SDCARD);
    }

    private void openWebService(int type) {
        if (deviceItem != null && !StringUtils.isEmpty(deviceItem.foreignServiceHook)){
            AbAppUtil.openServeByBrowser(this, deviceItem.foreignServiceHook);
        }else{
            ActivityHelper.openWebService(this, deviceItem, type);
        }

    }
}